package com.example.system.controller.exam;

import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.Result;
import com.example.common.core.domain.TableDataInfo;
import com.example.common.core.enums.ResultCode;
import com.example.system.domain.exam.dto.ExamAddDTO;
import com.example.system.domain.exam.dto.ExamEditDTO;
import com.example.system.domain.exam.dto.ExamQueryDTO;
import com.example.system.domain.exam.dto.ExamQuestionAddDTO;
import com.example.system.service.exam.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author 23050
 * @version 1.0
 * @description: 竞赛管理控制类
 * @date 2025/2/22 21:06
 */
@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private ExamService examService;



    /**
     * @author 23050
     * @description:  获取竞赛列表
     * @date: 2025/2/22 21:37
     * @param examQueryDTO
     * @return com.example.common.core.domain.TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo list(ExamQueryDTO examQueryDTO){
        return getTableDataInfo(examService.list(examQueryDTO));
    }


    /**
     * @author 23050
     * @description:  新增竞赛
     * @date: 2025/2/23 17:17
     * @return com.example.common.core.domain.Result
     * /system/exam/add
     * form-date格式不需要RequestBody
     */
    @PostMapping("/add")
    public Result add( ExamAddDTO examAddDTO){
        String ret = examService.add(examAddDTO);
        return new Result(ResultCode.SUCCESS.getCode(),"添加成功",ret);
    }

    /**
     * @author 23050
     * @description:  竞赛里面添加题目方法
     * @date: 2025/2/23 19:51
     * @param examQuestionAddDTO
     * @return com.example.common.core.domain.Result
     */
    @PostMapping("/question/add")
    public Result questionAdd(@RequestBody ExamQuestionAddDTO examQuestionAddDTO){
        boolean ret = examService.questionAdd(examQuestionAddDTO);
        if (ret){
            return new Result(ResultCode.SUCCESS.getCode(),"添加成功","");
        }
        return new Result(ResultCode.FAILED.getCode(),"添加失败","");
    }


    /**
     * @author 23050
     * @description:  删除竞赛中的题目
     * @date: 2025/3/2 17:06
     * @param examId
     * @param questionId
     * @return com.example.common.core.domain.Result
     */
    @DeleteMapping("/question/delete")
    public Result questionDelete(Long examId,Long questionId){
        int ret = examService.questionDelete(examId,questionId);
        if (ret>0){
            return new Result(ResultCode.SUCCESS.getCode(),"删除成功","");
        }
        return new Result(ResultCode.FAILED.getCode(),"删除失败","");
    }


    /**
     * @author 23050
     * @description:  竞赛详情获取
     * @date: 2025/3/1 19:57
     * @param examId
     * @return com.example.common.core.domain.Result
     */
    @GetMapping("/detail")
    public Result detail(Long examId){
        return new Result(ResultCode.SUCCESS.getCode(),"竞赛详情如下",examService.detail(examId));
    }


    /**
     * @author 23050
     * @description:  编辑竞赛基本信息
     * @date: 2025/3/1 21:51
     * @param examEditDTO
     * @return com.example.common.core.domain.Result
     * form-date格式不需要RequestBody
     */
    @PutMapping("/edit")
    public Result edit(ExamEditDTO examEditDTO){
        int ret = examService.edit(examEditDTO);
        if (ret>0){
            return new Result(ResultCode.SUCCESS.getCode(),"保存成功","");
        }
        return new Result(ResultCode.FAILED.getCode(),"保存失败","");
    }


    /**
     * @author 23050
     * @description:  删除竞赛
     * @date: 2025/3/2 18:11
     * @param examId
     * @return com.example.common.core.domain.Result
     */
    @DeleteMapping("/delete")
    public Result delete(Long examId){
        int ret = examService.deleteExam(examId);
        if (ret>0){
            return new Result(ResultCode.SUCCESS.getCode(),"删除成功","");
        }
        return new Result(ResultCode.FAILED.getCode(),"删除失败","");

    }


    /**
     * @author 23050
     * @description: 发布竞赛
     * @date: 2025/3/2 19:21
     * @param examId
     * @return com.example.common.core.domain.Result
     */
    @PutMapping("/publish")
    public Result publish(Long examId){
        int ret = examService.publish(examId);
        if (ret>0){
            return new Result(ResultCode.SUCCESS.getCode(),"发布成功","");
        }
        return new Result(ResultCode.FAILED.getCode(),"发布失败","");

    }

    /**
     * @author 23050
     * @description:  撤销发布
     * @date: 2025/3/2 19:33
     * @param examId
     * @return com.example.common.core.domain.Result
     */
    @PutMapping("/cancelPublish")
    public Result cancelPublish(Long examId){
        int ret = examService.cancelPublish(examId);
        if (ret>0){
            return new Result(ResultCode.SUCCESS.getCode(),"撤销成功","");
        }
        return new Result(ResultCode.FAILED.getCode(),"撤销失败","");

    }








}
